import fasttext
import jieba

# 训练词向量模型
model = fasttext.train_unsupervised('all_texts_cut.txt', model='skipgram')

# 保存模型
model.save_model("fasttext_model.bin")

def get_sentence_vector(sentence, model):
    words = jieba.cut(sentence)
    sentence_vector = model.get_sentence_vector(" ".join(words))
    return sentence_vector

# 计算两个句子的相似度
sentence1 = "这是一个示例句子"
sentence2 = "这是另一个示例文本"

vector1 = get_sentence_vector(sentence1, model)
vector2 = get_sentence_vector(sentence2, model)

# 使用向量之间的余弦相似度作为相似度度量
from scipy import spatial

similarity = 1 - spatial.distance.cosine(vector1, vector2)
print(f"相似度: {similarity}")
